//
//  RegisterViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()
@property (nonatomic, strong)UITextField *username;
@property (nonatomic, strong)UITextField *pwd;
@property (nonatomic, strong)UITextField *repwd;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title  = @"注册";
    self.username = [[UITextField alloc]initWithFrame:CGRectMake(60, 100, 250, 30)];
    _username.placeholder = @"请输入用户名";
    _username.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_username];
    [_username release];
    
    self.pwd = [[UITextField alloc]initWithFrame:CGRectMake(60, 160, 250, 30)];
    _pwd.placeholder = @"请输入密码";
    _pwd.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_pwd];
    [_pwd release];
    
    self.repwd = [[UITextField alloc]initWithFrame:CGRectMake(60, 220, 250, 30)];
    _repwd.placeholder = @"请确认密码";
    _repwd.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_repwd];
    [_repwd release];
    
    UIButton *registerLogin = [UIButton buttonWithType:UIButtonTypeSystem];
    registerLogin.frame = CGRectMake(140, 270, 90, 60);
    [registerLogin setTitle:@"注册" forState:UIControlStateNormal];
    [registerLogin addTarget:self action:@selector(registerAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerLogin];
    [registerLogin release];
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(returnAction)];
}
-(void)registerAction
{
    if (_username.text.length != 0 && [_pwd.text isEqualToString: _repwd.text] && _pwd.text.length != 0 ) {
        //把姓名写在单例里
        [[NSUserDefaults standardUserDefaults]setObject:_username.text forKey:@"name"];
        //弹窗
        UIAlertController *promptAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:promptAlert animated:YES completion:^{
            
        }];
        #pragma mark - 存储数据
        //在Caches中创建文件储存用户的姓名和密码
        //存储姓名
        NSString *namePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"name.a"];
        //从namepath路径解档获取数组
        NSMutableArray *nameArr = [NSKeyedUnarchiver unarchiveObjectWithFile:namePath];
        if (nameArr) {
            [nameArr addObject:_username.text];
        }else{
            nameArr = [NSMutableArray array];
            [nameArr addObject:_username.text];
        }
        //将数组归档
        [NSKeyedArchiver archiveRootObject:nameArr toFile:namePath];
        //存储密码
        NSString *pwdPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"pwd.a"];
        NSMutableArray *pwdArr = [NSKeyedUnarchiver unarchiveObjectWithFile:pwdPath];
        if (pwdArr) {
            [pwdArr addObject:_pwd.text];
        }else{
            pwdArr = [NSMutableArray array];
            [pwdArr addObject:_pwd.text];
        }
        [NSKeyedArchiver archiveRootObject:pwdArr toFile:pwdPath];
        //NSUserDefaults 设置登陆成功状态
        NSLog(@"%@", NSHomeDirectory());
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"register"];
        //让弹窗消失
        [self dismissViewControllerAnimated:YES completion:^{
            //
            [self.navigationController popToRootViewControllerAnimated:YES];
            NSLog(@"注册成功");
        }];
    }
}
-(void)returnAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
